<?
ob_start(); 
include("../common/header.html");
include('../common/autoload.php');
$out_html = ob_get_contents();
ob_end_clean();
include_once("../class/ProOrder.php");
set_time_limit(300);
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$soap = SoapInit::GetSoapInside();
$memberID = $_SESSION['sid'];
if ($memberID!=1) {
    exit('Access Denied!');
}
// 异步获取对应的景点门票
if($_SERVER['HTTP_REQUEST_TYPE']=='getTicket'){
	$sql='SELECT id,title FROM uu_jq_ticket WHERE landid='.($_REQUEST['landid']+0);//
	$GLOBALS['le']->query($sql);
	$tickes = array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$tickes[]=$row;
	}
	if(count($tickes)){
		echo json_encode(array('s'=>'success','content'=>$tickes));
	}else{
		echo json_encode(array('s'=>'fail'));
	}
	exit;
}

$btime=$_GET['begintime']?$_GET['begintime']:date("Y-m-d");
$etime=$_GET['endtime']?$_GET['endtime']:date("Y-m-d");
$by=$_GET['by']?$_GET['by']:"lid";
$option=array();
//if(count($_GET['status'])>0){
//	$option['status']=implode("|",$_GET['status']);
//}

// 景点查询
if(isset($_GET['landid']))	$option['lid'] = $_GET['landid'];

// 门票查询
if(isset($_GET['ticket'])){
	$t = explode("_",$_REQUEST['ticket']);$option['tid']=$t[0];
}
//交易时间类型
$timeType = isset($_GET['time_type'])? abs($_GET['time_type']) : 1;
//分销商ID
$fid = isset($_GET['fid']) ? abs($_GET['fid']) : 0;
$aid = isset($_GET['aid']) ? abs($_GET['aid']) : 0;

if($_GET['fid']=='' && $_GET['fid_str']){
    if($_GET['fid_str']+0>0) $fid = $_GET['fid_str'];
    else{
        $fid_str = mysql_real_escape_string($_GET['fid_str']);
        $sql = "select id from pft_member where instr(dname,'$fid_str') limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        $fid = $GLOBALS['le']->f('id');
    }
}

if($_GET['aid']=='' && $_GET['aid_str']){
    if($_GET['aid_str']+0>0) $aid = $_GET['aid_str'];
    else{
        $aid_str = mysql_real_escape_string($_GET['aid_str']);
        $sql = "select id from pft_member where instr(dname,'$aid_str') limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        $aid = $GLOBALS['le']->f('id');
    }
}


//TODO::直销
$option['n'] = 99999;
$obj = new AdminProOrder($memberID, $soap);

if($_GET['Order_Globle_Search_c']){
    $obj->handleRequest();
    $obj->params['sort'] ='';
    $obj->params['orderby'] = '';
    if($fid>0) $obj->params['mid'] = $fid;
    if($aid>0) $obj->params['aid'] = $aid;
    $obj->params['btime1'] = $obj->params['btime2'] = $obj->params['btime3']= $obj->params['etime1'] = $obj->params['etime2'] = $obj->params['etime3'] = '';
    switch($timeType) {
        case 2:
            $obj->params['btime2'] = $btime." 00:00:00";
            $obj->params['etime2'] = $etime." 23:59:59";
            break;
        case 3:
            $obj->params['btime3'] = $btime." 00:00:00";
            $obj->params['etime3'] = $etime." 23:59:59";
            break;
        default:
            $obj->params['btime1'] = $btime." 00:00:00";
            $obj->params['etime1'] = $etime." 23:59:59";
            break;
    }
    foreach ($option as $key => $value) {
        $obj->params[$key] = $value;
    }
    $r0 = $obj->totalOrders(2);
    $r0 = explode(',',$r0);
    $r[0]['title'] = '合计';
    $r[0]['order'] = $r0[0];
    $r[0]['tnum'] = $r0[1];
    $r[0]['money'] = $r0[2];
    goto no_select_range;
}
$titleT=array("lid"=>"景区名称","tid"=>"票类名称","mid"=>"分销商名称");
//已完成的订单数据 用已统计好的
if($timeType==3 && $btime<date('Y-m-d',$_SERVER['REQUEST_TIME'])){
    include_once '../module/zax/order_report/cetX.php';
    goto no_select_range;
}
if($_GET['select_range'] == '1' ){
    //只统计景区出售给下级的订单 包含自供自销
    $_GET['landid'] = $_GET['landid'] +0;
    if($_GET['landid'] == 0)
        exit ("<script>
                alert('请输入景区');
                history.go(-1);
            </script>");
    $sql = "select apply_did from uu_land where id={$_GET['landid']} limit 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $aid = $GLOBALS['le']->f('apply_did');
    $r0 = $obj->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, 0, $aid);
    $r1 = $obj->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, $aid, 0);
    $r = array_add($r0,$r1);
    unset($r0);
    unset($r1);
}elseif($_GET['select_range'] == '2' ){
    //统计这个供应商经手的所有订单
    $_GET['landid'] = $_GET['landid'] +0;
    if($_GET['landid'] == 0){
        exit ("<script>
                alert('请输入景区');
                history.go(-1);
            </script>");
    }else{
        //如果传入景区
        $sql = "select apply_did from uu_land where id={$_GET['landid']} limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        if($aid == $GLOBALS['le']->f('apply_did')){
            //如果是这个景区的供应商  用上面的.
            $r0 = $obj->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, 0, $aid);
            $r1 = $obj->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, $aid, 0);
            $r = array_add($r0,$r1);
            unset($r0);
            unset($r1);
        }else{
            //如果不是这个景区的供应商 用param['mid']=$_GET['aid']来查
            $r = $obj->_orderSum_admin_only_aid($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, $aid, 0);
        }
    }
}else{
    $r = $obj->_orderSum($btime." 00:00:00",$etime." 23:59:59",$by,$option, $timeType, $fid, $aid);
}


//会员数据
$aDistorsNames = include '../module/link_product/Member.cache.php';
if($_GET['by']=='mid' || $_GET['by']=='aid'){
    foreach($r as $k => $v){
        if($_SESSION['sid']==1 && $_SESSION['sdtype']==9){
            $r[$k]['title']=$aDistorsNames[$k];
        }else{
            $r[$k]['title']=$memberNames[$k];
        }
    }
}

//区分测试和非测试景区
if(is_array($r)){
    $dir = '/var/www/html/new/d/module/zax/admin_report/test_lid.txt';
    $a=filemtime($dir);
    if($_SERVER['REQUEST_TIME']-$a>86400){
        $sql = "select id from pft_member where group_id=2";
        $GLOBALS['le']->query($sql);
        $row = array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $r_fid[] = $row['id'];
        }
        $rm_fid = rtrim(json_encode($r_fid),']');
        $rm_fid = ltrim($rm_fid,'[');
        $sql = "select id from uu_land where apply_did in ($rm_fid)";
        $GLOBALS['le']->query($sql);
        $row = array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $r_lid[] = (int)$row['id'];
        }
        $r_lid[] = 10626;//慌慌的淘宝产品
        $r_lid[] = 7714;//测试微信导览
        file_put_contents($dir,json_encode($r_lid));
    }else{
        $r_lid = json_decode(file_get_contents($dir),true);
    }
    switch ($_GET['is_test']) {
    	case '1'://测试帐号
            case1:
    		foreach($r as $k => $v){
                if(!in_array($v['lid'],$r_lid))
                    unset($r[$k]);
            }
    		break;

    	case '2'://非测试帐号
            foreach($r as $k => $v){
                if(in_array($v['lid'],$r_lid))
                    unset($r[$k]);
            }
    		break;
        default:
            goto case1;
    }
}


$totals=array();
/*导出EXCEL处理程序--begin-*/

include("../ExcelHandle.php");
/*导出EXCEL处理程序--end*/

$from="sale";
// 获取产品
/*
$products = array(); //系统中的所有在售产品

$sql = "SELECT id,title,getPy(title,1) as py FROM uu_land WHERE status=1 ORDER BY py ASC";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
    if (!is_numeric($row['py'])) {
        $products[$row['id']] = $row['py'] . '-'. $row['title'];
    }
}
*/
no_select_range:
echo $out_html;
include("orderReport.tpl");
include("../common/footer.html");

function array_add($r0,$r1){
    foreach($r0 as $k => $v){
        $r[$k]['title'] = $r0[$k]['title']? $r0[$k]['title'] : $r1[$k]['title'];
        $r[$k]['order'] = $r1[$k]['order']+$r0[$k]['order'];
        $r[$k]['tnum'] = $r1[$k]['tnum']+$r0[$k]['tnum'];
        $r[$k]['money'] = $r1[$k]['money']+$r0[$k]['money'];
        $r[$k]['pmode0'] = $r1[$k]['pmode0']+$r0[$k]['pmode0'];
        $r[$k]['pmode1'] = $r1[$k]['pmode1']+$r0[$k]['pmode1'];
        $r[$k]['pmode2'] = $r1[$k]['pmode2']+$r0[$k]['pmode2'];
        $r[$k]['pmode3'] = $r1[$k]['pmode3']+$r0[$k]['pmode3'];
        $r[$k]['pmode4'] = $r1[$k]['pmode4']+$r0[$k]['pmode4'];
    }
    foreach($r1 as $k=> $v){
        if($r[$k]) continue;
        $r[$k]['title'] = $r1[$k]['title'];
        $r[$k]['order'] = $r1[$k]['order'];
        $r[$k]['tnum'] = $r1[$k]['tnum'];
        $r[$k]['money'] = $r1[$k]['money'];
        $r[$k]['pmode0'] = $r1[$k]['pmode0'];
        $r[$k]['pmode1'] = $r1[$k]['pmode1'];
        $r[$k]['pmode2'] = $r1[$k]['pmode2'];
        $r[$k]['pmode3'] = $r1[$k]['pmode3'];
        $r[$k]['pmode4'] = $r1[$k]['pmode4'];
    }
    return $r;
}
?>